package com.example.springboot.utils;

import java.security.*;
import java.util.Base64;

/**
 * 生成Jwt私钥公钥
 */
public class RSAKeyUtil {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 使用 KeyPairGenerator 生成 RSA 密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048); // 2048 位的密钥长度
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        // 获取生成的 RSA 密钥对
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 将公钥和私钥转换为 Base64 编码的字符串
        String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKey.getEncoded());
        String privateKeyBase64 = Base64.getEncoder().encodeToString(privateKey.getEncoded());

        // 输出生成的密钥对
        System.out.println("公钥:");
        System.out.println(publicKeyBase64);
        System.out.println("\n私钥:");
        System.out.println(privateKeyBase64);
    }
}
